package com.jjkj.environmental.environmental.model

import com.jjkj.environmental.environmental.api.CarCheckApi
import com.jjkj.environmental.environmental.bean.model.*
import com.jjkj.environmental.environmental.bean.request.Condition
import com.jjkj.environmental.environmental.bean.request.DvrService
import com.jjkj.environmental.environmental.bean.request.QueryRequest
import com.jjkj.environmental.environmental.bean.request.WriteRequest
import com.jjkj.environmental.environmental.utils.LitePalHelper
import com.jjkj.environmental.environmental.utils.Retrofit2Utils
import com.jjkj.environmental.environmental.utils.RxHelper
import com.jjkj.environmental.environmental.utils.Setting
import com.jjkj.environmental.environmental.utils.pull.PullConverter
import io.reactivex.Observable
import io.reactivex.Observer

class NetWorkModel : RxHelper {
    private var serviceApi: CarCheckApi = Retrofit2Utils.getServiceApi(CarCheckApi::class.java, LitePalHelper.getUrl())


    fun updateIpAddress(ipAddress: String) {
        val url = Setting.appendUrl(ipAddress)
        this.serviceApi = Retrofit2Utils.getServiceApi(CarCheckApi::class.java, url = url)
    }


    fun login(observer: Observer<String>, userInfo: UserInfo) {
        Observable.just(userInfo)
                .map { Condition(it) }
                .map { PullConverter.convertCondition(it) }
                .map { QueryRequest(Setting.API19C06, it) }
                //请求登录接口
                .flatMap { serviceApi.queryCheckInfoWithPull(it) }
                .map { it.body!! }
                .compose(bindOb())
                .subscribe(observer)
    }

    fun loadCatList(observer: Observer<List<CarInfo>>, checkStation: CheckStation) {
        Observable.just(checkStation)
                .map { Condition(it) }
                .map { PullConverter.convertCondition(it) }
                .map { QueryRequest(Setting.API19C21, it) }
                //请求查询列表接口
                .flatMap { serviceApi.queryCheckInfoWithPull(it) }
                .map { it.body!! }
                .map { PullConverter.xml2CarInfoList(it) }
                .compose(bindOb())
                .subscribe(observer)
    }

    fun makePicture(observer: Observer<String>, carInfo: CarInfo) {
        Observable.just(carInfo)
                .map { DvrRequest(it) }
                .map { Condition(it) }
                .map { PullConverter.convertCondition(it) }
                .map { DvrService(Setting.API19S03, it) }
                //请求拍照接口
                .flatMap { serviceApi.queryCheckInfoWithPull(it) }
                .map { it.body!! }
                .map { DvrRequest(carInfo) }
                .map { Condition(it) }
                .map { PullConverter.convertCondition(it) }
                .map { DvrService(Setting.API19S01, it) }
                //请求摄像头开始接口
                .flatMap { serviceApi.queryCheckInfoWithPull(it) }
                .map { it.body!! }
                .compose(bindOb())
                .subscribe(observer)
    }

    fun closeVideo(observer: Observer<String>, carInfo: CarInfo) {
        Observable.just(carInfo)
                .map { DvrRequest(it) }
                .map { Condition(it) }
                .map { PullConverter.convertCondition(it) }
                .map { DvrService(Setting.API19S02, it) }
                //请求摄像头结束接口
                .flatMap { serviceApi.queryCheckInfoWithPull(it) }
                .map { it.body!! }
                .compose(bindOb())
                .subscribe(observer)
    }

    fun uploadPicture(observer: Observer<String>, carInfo: CarInfo, path: String, zpzl: String) {
        Observable.just(carInfo)
                .map { PictureUpLoad(it, path, zpzl) }
                .map { Condition(it) }
                .map { PullConverter.convertCondition(it) }
                .map { WriteRequest(Setting.API19C33, it) }
                //请求摄像头结束接口
                .flatMap { serviceApi.queryCheckInfoWithPull(it) }
                .map { it.body!! }
                .compose(bindOb())
                .subscribe(observer)
    }

    fun uploadCheckInfo(observer: Observer<String>, carInfo: CarInfo, checkInfo: CheckInfo) {
        Observable.just(1)
                .map { CheckInfoUpload(carInfo, checkInfo) }
                .map { Condition(it) }
                .map { PullConverter.convertCondition(it) }
                .map { WriteRequest(Setting.API19C30, it) }
                //请求上传外检信息接口
                .flatMap { serviceApi.queryCheckInfoWithPull(it) }
                .map { it.body!! }
                .compose(bindOb())
                .subscribe(observer)
    }

}